package com.mz.ozy.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.net.Uri;
import android.os.Environment;
import android.util.Base64;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.mz.ozy.R;
import com.mz.ozy.widget.MyGlideEngine;
import com.yancy.imageselector.ImageConfig;
import com.yancy.imageselector.ImageSelector;
import com.zhihu.matisse.Matisse;
import com.zhihu.matisse.MimeType;
import com.zhihu.matisse.internal.entity.CaptureStrategy;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

/**
 * Created by Administrator on 2017/7/28.
 */

public class BitmapUtils {
    public static String bitmapToBase64(Bitmap bitmap) {

        String result = null;
        ByteArrayOutputStream baos = null;
        try {
            if (bitmap != null) {
                baos = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.JPEG, 60, baos);

                baos.flush();
                baos.close();

                byte[] bitmapBytes = baos.toByteArray();
                result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (baos != null) {
                    baos.flush();
                    baos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    /**
     * 将 路径转换为 bitmap
     * 根据图片路径 处理后返回bitmap
     * 相对于直接使用 BitmapFactory.decodeFile(filePath); 有效防止OOM
     *
     * @param filePath  文件路径
     * @param zoomLevel 缩放级别（建议是2的整倍数）
     * @return
     */
    public static Bitmap pathToBitmap(String filePath, int zoomLevel) {
        try {
            InputStream is = new FileInputStream(filePath);
            return inputstremToBitmap(is, zoomLevel);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Bitmap createBitmap1(View view) {
        Bitmap temBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(temBitmap);
        view.draw(canvas);
        return temBitmap;
    }

    /**
     * 将 inputStrem 转换为bitmap
     *
     * @param is        文件字节流
     * @param zoomLevel 缩放级别（建议为2的整倍数）
     * @return
     */
    public static Bitmap inputstremToBitmap(InputStream is, int zoomLevel) {
        try {
            BitmapFactory.Options opts = new BitmapFactory.Options();
            // 内存中申请100k缓存空间
            opts.inTempStorage = new byte[100 * 1024];
            opts.inPreferredConfig = Bitmap.Config.RGB_565;
            //设置图片可以被回收，创建Bitmap用于存储Pixel的内存空间在系统内存不足时可以被回收
            opts.inPurgeable = true;
            //设置位图缩放比例 width，hight设为原来的四分一（该参数请使用2的整数倍）
            // 这也减小了位图占用的内存大小；例如，一张分辨率为2048*1536px的图像使用inSampleSize值为4的设置来解码，产生的Bitmap大小约为512*384px。相较于完整图片占用12M的内存，这种方式只需0.75M内存(假设Bitmap配置为ARGB_8888)。
            opts.inSampleSize = zoomLevel;
            //设置解码位图的尺寸信息
            opts.inInputShareable = true;
            //解码位图
            Bitmap bitmap = BitmapFactory.decodeStream(is, null, opts);
            // 返回所需bitmap
            return bitmap;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Bitmap inputstremToBitmap(String filePath) {
        try {
          /*  BitmapFactory.Options opts = new BitmapFactory.Options();
            // 内存中申请100k缓存空间
            opts.inTempStorage = new byte[100 * 1024];
            opts.inPreferredConfig = Bitmap.Config.RGB_565;
            //设置图片可以被回收，创建Bitmap用于存储Pixel的内存空间在系统内存不足时可以被回收
            opts.inPurgeable = true;

            //设置位图缩放比例 width，hight设为原来的四分一（该参数请使用2的整数倍）
            // 这也减小了位图占用的内存大小；例如，一张分辨率为2048*1536px的图像使用inSampleSize值为4的设置来解码，产生的Bitmap大小约为512*384px。相较于完整图片占用12M的内存，这种方式只需0.75M内存(假设Bitmap配置为ARGB_8888)。
            opts.inSampleSize = zoomLevel;
            //设置解码位图的尺寸信息
            opts.inInputShareable = true;
            //解码位图
            Bitmap bitmap = BitmapFactory.decodeStream(is, null, opts);
            // 返回所需bitmap
            return bitmap;*/

            BitmapFactory.Options opts = new BitmapFactory.Options();
            opts.inJustDecodeBounds = true;
            Bitmap bmp = BitmapFactory.decodeFile(filePath, opts);
            opts.inJustDecodeBounds = false;

            int w = opts.outWidth;
            int h = opts.outHeight;
            float standardW = 600f;
            float standardH = 600f;

            int zoomRatio = 1;
            if (w > h && w > standardW) {
                zoomRatio = (int) (w / standardW);
            } else if (w < h && h > standardH) {
                zoomRatio = (int) (h / standardH);
            }
            if (zoomRatio <= 0)
                zoomRatio = 1;
            opts.inSampleSize = zoomRatio;
            bmp = BitmapFactory.decodeFile(filePath, opts);
            return bmp;

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    // 缩放图片
    public static Bitmap zoomImg(Bitmap bm, int newWidth, int newHeight) {
        // 获得图片的宽高
        int width = bm.getWidth();
        int height = bm.getHeight();
        // 计算缩放比例
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        // 取得想要缩放的matrix参数
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        // 得到新的图片
        Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
        return newbm;
    }


    /**
     * 保存到指定位置，只是暂时存储
     *
     * @param bitmap
     * @param fileName
     * @return
     */
    public static String saveImageSdcard(Bitmap bitmap, String fileName) {
        // 判断是否可以对SDcard进行操作
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            // 获取SDCard指定目录下
            String sdCardDir = Environment.getExternalStorageDirectory() + "/OZY/temp/";
            File dirFile = new File(sdCardDir);   //目录转化成文件夹
            if (!dirFile.exists()) {              //如果不存在，那就建立这个文件夹
                dirFile.mkdirs();
            }                          //文件夹有啦，就可以保存图片啦
            File file = new File(sdCardDir, fileName + ".png");// 在SDcard的目录下创建图片文,以当前时间为其命名
            FileOutputStream out = null;
            try {
                out = new FileOutputStream(file);
                //通过io流的方式来压缩保存图片
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } finally {
                try {
                    out.flush();
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            LogUtils.e("QINIU", "保存已经至" + file.getAbsolutePath() + "目录文件夹下");
            return file.getAbsolutePath();
        }
        return "";
    }


    /**
     * 保存图片
     *
     * @param bm
     * @param path
     * @param fileName
     * @return
     * @throws IOException
     */
    public static File saveFile(Bitmap bm, String path, String fileName) throws IOException {
        File dirFile = new File(path);
        if (!dirFile.exists()) {
            dirFile.mkdir();
        }
        File myCaptureFile = new File(path, fileName);
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
        bm.compress(Bitmap.CompressFormat.JPEG, 100, bos);
        bos.flush();
        bos.close();
        return myCaptureFile;
    }

    //保存文件到指定路径
    public static Uri saveImageToGallery(Context context, Bitmap bmp) {
        // 首先保存图片
        String storePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "OZYShop";
        File appDir = new File(storePath);
        if (!appDir.exists()) {
            appDir.mkdir();
        }
        String fileName = System.currentTimeMillis() + ".jpg";
        File file = new File(appDir, fileName);
        try {
            FileOutputStream fos = new FileOutputStream(file);
            //通过io流的方式来压缩保存图片
            boolean isSuccess = bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();

            //把文件插入到系统图库
            //MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), fileName, null);

            //保存图片后发送广播通知更新数据库
            Uri uri = Uri.fromFile(file);
            context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri));
            if (isSuccess) {
                return uri;
            } else {
                return null;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Matisse 选择图片
     *
     * @param context
     * @param max         最大数量
     * @param requestCode 请求码
     */
    public static void selectPhoto1(Activity context, int max, int requestCode) {
        Matisse.from(context)
                .choose(MimeType.ofImage())
                .countable(true)   //true:选中后显示数字;false:选中后显示对号
                .maxSelectable(max)  //可选的最大数
                .capture(true)//选择照片时，是否显示拍照
                .captureStrategy(new CaptureStrategy(true, ProviderUtil.getFileProviderName(context)))//参数1 true表示拍照存储在共有目录，false表示存储在私有目录；参数2与 AndroidManifest中authorities值相同，用于适配7.0系统 必须设置
                .imageEngine(new MyGlideEngine())//图片加载引擎
                .forResult(requestCode);
    }

    public static void selectPhoto(Activity context, boolean isSingle, int max, ArrayList<String> images) {
        ImageConfig.Builder builder = new ImageConfig.Builder(new GlideLoader())
                .steepToolBarColor(context.getResources().getColor(R.color.c_ff0f47))
                .titleBgColor(context.getResources().getColor(R.color.c_ff0f47))
                .titleSubmitTextColor(context.getResources().getColor(R.color.white))
                .titleTextColor(context.getResources().getColor(R.color.white))
                .showCamera()
                //.crop()
                .filePath("/ImageSelector/Pictures");
        if (isSingle) {
            builder.singleSelect();
        } else {
            builder.mutiSelect().mutiSelectMaxSize(max).pathList(images);
        }
        ImageConfig imageConfig = builder.build();

        ImageSelector.open(context, imageConfig);   // 开启图片选择器

    }

    /**
     * 图片名
     *
     * @return
     */
    public static String getPictureName() {
        return "ozy" + System.currentTimeMillis();
    }

    public static class GlideLoader implements com.yancy.imageselector.ImageLoader {

        @Override
        public void displayImage(Context context, String path, ImageView imageView) {
            RequestOptions options = new RequestOptions()
                    .centerCrop()
                    .placeholder(com.yancy.imageselector.R.mipmap.imageselector_photo)
                    .diskCacheStrategy(DiskCacheStrategy.ALL);   //在load()和into()之间，可以串连添加各种功能。
            Glide.with(context)
                    .load(path)
                    .apply(options)
                    .into(imageView);
        }

    }

    public static Bitmap createBitmap(View view) {

        view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
        view.buildDrawingCache();
        Bitmap bitmap = view.getDrawingCache();
        return bitmap;
    }

}
